<template>
	<a-card :bordered="false" style="width: 100%">
		<a-form ref="searchFormRef" :model="searchFormState">
			<a-row :gutter="10">
				<a-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
					<a-form-item label="数字ID" name="palyId">
						<a-input v-model:value="searchFormState.palyId" placeholder="请输入数字ID" />
					</a-form-item>
				</a-col>
				<a-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
					<a-form-item label="昵称" name="nickname">
						<a-input v-model:value="searchFormState.nickname" placeholder="请输入昵称" />
					</a-form-item>
				</a-col>
				<a-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
					<a-form-item label="能力值" name="ability">
						<a-input-number v-model:value="searchFormState.ability" placeholder="请输入能力值 (1-6星)" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
				    <a-form-item>
				        <a-space>
				            <a-button type="primary" @click="tableRef.refresh(true)">
				                <template #icon><SearchOutlined /></template>
				                查询
				            </a-button>
				            <a-button @click="reset">
				                <template #icon><redo-outlined /></template>
				                 重置
				            </a-button>
                        </a-space>
                    </a-form-item>
                </a-col>
            </a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
			:scroll="{ x: 'max-content' }"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('playerAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<a-button @click="importModelRef.onOpen()" v-if="hasPerm('playerImport')">
                        <template #icon><import-outlined /></template>
                        <span>导入</span>
                    </a-button>
                    <a-button @click="exportData" v-if="hasPerm('playerExport')">
                        <template #icon><export-outlined /></template>
                        <span>导出</span>
                    </a-button>
					<xn-batch-button
						v-if="hasPerm('playerBatchDelete')"
						buttonName="批量删除"
						icon="DeleteOutlined"
						buttonDanger
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchPlayer"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'positions'">
					<a-space wrap>
						<a-tag v-for="pos in parsePositions(record.positions || record.position || record.goodPositions)" :key="pos">
							{{ $TOOL.dictTypeData('player_position', pos) || pos }}
						</a-tag>
					</a-space>
				</template>
				<template v-else-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('playerEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['playerEdit', 'playerDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deletePlayer(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('playerDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<ImportModel ref="importModelRef" />
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="player">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import ImportModel from './importModel.vue'
	import downloadUtil from '@/utils/downloadUtil'
	import playerApi from '@/api/dota/playerApi'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const importModelRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '数字 ID',
			dataIndex: 'palyId',
			width: 120,
			align: 'center'
		},
		{
			title: ' 昵称',
			dataIndex: 'nickname',
		},
		{
			title: 'MMR',
			dataIndex: 'mmr',
			align: 'center'
		},
		{
			title: '擅长位置',
			dataIndex: 'positions',
			align: 'center'
		},
		{
			title: '能力值',
			dataIndex: 'ability',
			align: 'center'
		},
		{
			title: '词条属性',
			dataIndex: 'tags',
			align: 'center'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['playerEdit', 'playerDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			fixed: 'right'
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const parsePositions = (value) => {
		if (!value) {
			return []
		}
		if (Array.isArray(value)) {
			return value
		}
		return String(value)
			.split(',')
			.map((item) => item.trim())
			.filter((item) => item)
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return playerApi.playerPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deletePlayer = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		playerApi.playerDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 导出
    const exportData = () => {
        if (selectedRowKeys.value.length > 0) {
            const params = selectedRowKeys.value.map((m) => {
                return {
                    id: m
                }
            })
            playerApi.playerExport(params).then((res) => {
                downloadUtil.resultDownload(res)
            })
        } else {
            playerApi.playerExport([]).then((res) => {
                downloadUtil.resultDownload(res)
            })
        }
    }
	// 批量删除
	const deleteBatchPlayer = (params) => {
		playerApi.playerDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
